package com.quasar.hpatient.view.personal.historyRecord;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.widget.Toolbar;

import com.change360.calendarview.CalendarDay;
import com.change360.calendarview.MaterialCalendarView;
import com.change360.calendarview.OnDateChangedListener;
import com.change360.calendarview.OnMonthChangedListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.custommodel.Record;
import com.quasar.hpatient.model.custommodel.RecordState;
import com.quasar.hpatient.model.event.EventDecorator;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.medicalmodel.DailyRecordData;
import com.quasar.hpatient.onInterface.OnHistoryDailyListenter;
import com.quasar.hpatient.presenter.HistoryDailyDataPresenter;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.record.MoreRecodeActivity_;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * 历史日常记录
 */
@EActivity(R.layout.activity_history_record_day)
public class HistoryRecordDayActivity extends BaseActivity implements OnDateChangedListener, OnMonthChangedListener, OnHistoryDailyListenter {


    CalendarDay calendarDay;
    private HistoryDailyDataPresenter historyDailyDataPresenter;
    List<CalendarDay> calendarLaboratoryList = new ArrayList<CalendarDay>();
    List<DailyRecordData> testPresentation = new ArrayList<>();

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    @ViewById(R.id.calendarView)
    MaterialCalendarView calendarView;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
    }

    @Override
    protected void initData() {

        historyDailyDataPresenter = new HistoryDailyDataPresenter(this);
        //初始化日期
        calendarView.setCurrentDate(Calendar.getInstance());
        calendarView.setCurrentDate(new Date());
        calendarView.setSelectedDate(new Date());
        calendarView.setOnDateChangedListener(this);
        calendarView.setOnMonthChangedListener(this);
        calendarView.setMaximumDate(new Date());
        calendarDay = new CalendarDay();


    }

    @Override
    public void onDateChanged(MaterialCalendarView widget, CalendarDay date) {
//        HistoryRecordListActivity_.intent(this).TimeDate(TimeZoneUtil.sdf.format(date.getDate())).start();

        MoreRecodeActivity_.intent(this).TimeDate(TimeZoneUtil.sdf.format(date.getDate())).start();
        calendarDay = date;
    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
        historyDailyDataPresenter.GetPatientDailyRecords(TimeZoneUtil.fastMoth(date), TimeZoneUtil.lastMoth(date));
    }

    @Override
    protected void onResume() {
        super.onResume();
        historyDailyDataPresenter.GetPatientDailyRecords(TimeZoneUtil.fastMoth(calendarDay), TimeZoneUtil.sdf.format(new Date()));
    }

    @Override
    public void onViewDataSuccess(AnotherResult<DailyRecordData> data) {

        if (data.getList() != null && data.getList().size() > 0) {
            new ApiSimulator(data.getList()).executeOnExecutor(Executors.newSingleThreadExecutor(), calendarDay);
        }

    }

    @Override
    public void onViewError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onViewRefreshUI() {

    }

    private class ApiSimulator extends AsyncTask<CalendarDay, Void, List<RecordState>> {

        private List<DailyRecordData> dailyRecordDataList;

        ArrayList<RecordState> csdates = new ArrayList<>();
        ArrayList<RecordState> lfdates = new ArrayList<>();

        public ApiSimulator(List<DailyRecordData> dailyRecordDataList) {
            this.dailyRecordDataList = dailyRecordDataList;
        }

        @Override
        protected List<RecordState> doInBackground(CalendarDay... voids) {

            ArrayList<RecordState> dates = new ArrayList<>();

            for (int i = 0; i < dailyRecordDataList.size(); i++) {
                RecordState recordState = new RecordState();
                recordState.drawable = R.mipmap.rc;
                Date de = null;
                try {
                    de = TimeZoneUtil.sdf.parse(dailyRecordDataList.get(i).getDailyLogEndTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Calendar c = Calendar.getInstance();
                c.setTime(de);
                recordState.calendarDay = new CalendarDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                dates.add(recordState);

                for (int j = 0; j < dailyRecordDataList.size(); j++) {
                    Calendar datec = Calendar.getInstance();
                    datec.setTime(new Date());

                    ViseLog.d("数据的时间"+dailyRecordDataList.get(i).getDailyLogEndTime());
                    if (dailyRecordDataList.get(i).getWarn() ==1) {

                        ViseLog.d("异常的数据"+dailyRecordDataList.get(i).getDailyLogEndTime()+"数据的长度"+dailyRecordDataList.size());
                        RecordState xxxx = new RecordState();
                        Date des = null;
                        try {
                            des = TimeZoneUtil.sdf.parse(dailyRecordDataList.get(i).getDailyLogEndTime());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar b = Calendar.getInstance();
                        b.setTime(des);
                        xxxx.calendarDay = new CalendarDay(b.get(Calendar.YEAR), b.get(Calendar.MONTH), b.get(Calendar.DAY_OF_MONTH));
                        csdates.add(xxxx);
                        continue;
                    }else{
                        ViseLog.d("正常的数据"+dailyRecordDataList.get(i).getDailyLogEndTime()+"数据的长度"+dailyRecordDataList.size());
                        RecordState xxxx = new RecordState();
                        Date des = null;
                        try {
                            des = TimeZoneUtil.sdf.parse(dailyRecordDataList.get(i).getDailyLogEndTime());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar b = Calendar.getInstance();
                        b.setTime(des);
                        xxxx.calendarDay = new CalendarDay(b.get(Calendar.YEAR), b.get(Calendar.MONTH), b.get(Calendar.DAY_OF_MONTH));
                        lfdates.add(xxxx);
                        continue;
                    }
                }
            }
            return dates;
        }

        @Override
        protected void onPostExecute(List<RecordState> calendarDays) {
            super.onPostExecute(calendarDays);
            List<CalendarDay> days = new ArrayList<>();

            for (int i = 0; i < calendarDays.size(); i++) {
                RecordState state = calendarDays.get(i);
                days.add(state.calendarDay);
            }
            Drawable drawable = getResources().getDrawable(R.mipmap.rc);
            calendarView.addDecorator(new EventDecorator(drawable, days));

            List<CalendarDay> csdays = new ArrayList<>();
            for (int i = 0; i < csdates.size(); i++) {
                RecordState state = csdates.get(i);
                csdays.add(state.calendarDay);
            }
            calendarView.addDecorator(new EventDecorator(getResources().getDrawable(R.mipmap.rc_h), csdays));



            List<CalendarDay> lfdays = new ArrayList<>();
            for (int i = 0; i < lfdates.size(); i++) {
                RecordState state = lfdates.get(i);
                lfdays.add(state.calendarDay);
            }
            calendarView.addDecorator(new EventDecorator(getResources().getDrawable(R.mipmap.rc), lfdays));
        }
    }

}
